package cn.learn.file;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @author huangyezhan
 * @version 1.0
 * @date 2020年09月23日18:41
 */
public class ImportFileData {

    /**
     * 字符流
     *
     * @param path 路径
     * @return 参数
     */
    public static List<String> BrTest(String path) {
        List<String>   list = new ArrayList<>();
        BufferedReader br   = null;
        try {
            br = new BufferedReader(new FileReader(path));
            String str = null;
            while ((str = br.readLine()) != null) {
                list.add(str);
            }

           /* CharBuffer cb = CharBuffer.allocate(1024);
            while (br.read(cb) != -1) {
                cb.flip();//注意，这里不调会死循环。。。flip()将缓存字节数组的指针设置为数组的开始序列即数组下标0。这样就可以从buffer开头，对该buffer进行读取。
                list.add(cb.toString());
            }*/
        } catch (Exception e) {
            e.printStackTrace();
        } finally {//关掉IO
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    /**
     * 生成指定位数的随机字符串
     *
     * @param numLength  数据的长度
     * @param charLength 字母的长度
     * @return 返回生成的字符串
     */
    public static String getRandomStr(int numLength, int charLength) {
        StringBuilder sb     = new StringBuilder();
        Random        random = new Random();

        //拼接数字
        if (numLength > 0) {
            String numChars = "0123456789";
            int    len      = numChars.length();
            for (int i = 0; i < numLength; i++) {
                int nextInt = random.nextInt(len);
                sb.append(numChars.charAt(nextInt));
            }
        }
        //拼接字符串
        if (charLength > 0) {
            String chars = "QWERTYUIOPASDFGHJKLZXCVBNM";
            int    len   = chars.length();
            for (int i = 0; i < charLength; i++) {
                int nextInt = random.nextInt(len);
                sb.append(chars.charAt(nextInt));
            }
        }
        return sb.toString();
    }

    /**
     * 生成脱敏的姓名
     *
     * @param familyName 姓的集合
     * @return 返回姓名
     */
    public static String createName(List<String> familyName) {
        if (familyName != null && familyName.size() > 0) {
            Random random  = new Random();
            int    nextInt = random.nextInt(familyName.size());
            return familyName.get(nextInt) + "**";
        }
        return "***";
    }

    /**
     * @param multiple 获取多少位整数
     * @return 返回字符串的浮点数值
     */
    public static double createDouble(int multiple) {
        multiple = multiple > 0 ? multiple : 1;
        int    i      = (int) Math.pow(10, multiple);
        Random random = new Random();
        double v      = random.nextDouble() * i;
        //3.#.00表示保留后两位，它的处理方式是直接截掉不要的尾数，不四舍五入。
        DecimalFormat data   = new DecimalFormat("#.00");
        String        format = data.format(v);
        return v;
    }

    /**
     * 随机生成时间
     *
     * @return 返回时间字符串
     */
    public String randomDate() {
        Random rndYear = new Random();
        //生成[2018,2020]的整数；年
        int year = rndYear.nextInt(2) + 2018;

        //生成[1,12]的整数；月
        Random rndMonth = new Random();
        int    month    = rndMonth.nextInt(12) + 1;

        //生成[1,30)的整数；日
        Random rndDay = new Random();
        int    Day    = rndDay.nextInt(30) + 1;

        //生成[0,23)的整数；小时
        Random rndHour = new Random();
        int    hour    = rndHour.nextInt(23);

        //生成分钟
        Random rndMinute = new Random();
        int    minute    = rndMinute.nextInt(60);

        //秒
        Random rndSecond = new Random();
        int    second    = rndSecond.nextInt(60);

        return year + "-" + month + "-" + Day + "  " + hour + ":" + minute + ":" + second;
    }


    public static String tjny() {
        Random rndYear = new Random();
        //生成[2018,2020]的整数；年
        int year = /*rndYear.nextInt(2) + */2020;
        //生成[1,12]的整数；月
        Random rndMonth = new Random();
        int    month    = /*rndMonth.nextInt(12) + 1*/ 5;
        String m        = month > 9 ? "" + month : "0" + month;

        return year + "" + m;
    }

    public static String createDay() {
        Random random = new Random();
        //生成[2018,2020]的整数；年
        int year = random.nextInt(2) + 2019;
        //生成[1,12]的整数；月
        int    month = random.nextInt(12) + 1;
        String m     = month > 9 ? "" + month : "0" + month;

        int    day = random.nextInt(30) + 1;
        String d   = day > 9 ? "" + day : "0" + day;

        return year + "-" + m + "-" + d;
    }

    public static Date ltDay(String dayStr) {
        try {
            Random random = new Random();

            String tjny = ltTjny(dayStr);
            int    day  = random.nextInt(30) + 1;
            String d    = day > 9 ? "" + day : "0" + day;
            tjny = tjny + d;

            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            return sdf.parse(tjny);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 时间小于统计年月
     */
    public static String ltTjny(String tjny) {
        int y = Integer.parseInt(tjny.substring(0, 4));
        int m = Integer.parseInt(tjny.substring(4));

        Random rndYear = new Random();
        int    year    = rndYear.nextInt(2) + 2019;

        Random rndMonth = new Random();
        int    month    = rndMonth.nextInt(12) + 1;
        if (y > year) {
            month = rndMonth.nextInt(12) + 1;
        } else {
            if (m - month < 1) {
                y = y - 1;
            }
        }

        return Math.min(year, y) + (month > 9 ? "" + month : "0" + month);
    }

    public static String address(String address) {
        if (address.length() > 4) {
            return address.substring(0, 2) + "******" + address.substring(address.length() - 4);
        } else {
            return address.substring(0, 1) + "**";
        }
    }

    public static int gtInteger(int value){
        Random random = new Random();
        int    i      = random.nextInt(1000000);
        return value + i;
    }

    public static void main(String[] args) {
        String tjny = "201902";
        System.out.println(tjny.substring(0, 4));
        System.out.println(tjny.substring(4));

    }

}
